<template>
  <!-- 库位定义 -->
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" :collapse="false" @filterChange="handleFilterChange" />
    <FilterTable
      ref="table"
      columnsRef="partPartInfoManageLibraryDefinitionIndex"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addMoreHandle">批量新增</el-button>
        <el-button type="primary" @click="showImportFun">库位导入</el-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <libraryModel :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
    <!-- 批量新增 -->
    <BaseDialog :visible.sync="visible1" title="批量新增" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addMoreModel :type="handleType" :rowData="formData" @close="closeHandler" @researchTable="handleResearchTable" />
    </BaseDialog>

    <BaseDialog :visible.sync="showImport" title="导入" :width="'30%'" :destroyOnClose="true" :containerStyle="{ height: 'calc(100% - 120px)', overflow: 'auto', paddingBottom: '60px' }">
      <importModel @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getLibrary, getBrands, updateStatus, getAllSaler } from '@/api/part/Warehouse';
import { getDealerUser } from '@/api/customerRepair/customerComplaint/customerComplaintReception';
import libraryModel from './libraryModel';
import addMoreModel from './addMoreModel';
import importModel from './importModel.vue';
import { getStroageList } from '@/api/part/partOutbound/partBreak';
import { getStorageList } from '@/api/part/common';
import { findAllRepair } from '@/api/repair/basicParameters';
import { getAllUser } from '@/api/customerRepair/afterSaleVisit/resultEntry';
export default {
  components: {
    libraryModel,
    importModel,
    addMoreModel
  },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    return {
      fetchParams: { xhrAbort: true },
      visible: false,
      visible1: false,
      showImport: false,
      dialogTitle: '',
      fetchApi: getLibrary,
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      serviceAdvisorData: [],
      salerList: {} // 销售列表(翻译用
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.getBrands();
    this.getSalerList();
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" disabled={props.row.isValid == 10041001} type="text" onClick={() => this.handleUpdateStatus(props.row)}>
                  启用
                </el-button>
                <el-button size="mini" disabled={props.row.isValid != 10041001} type="text" onClick={() => this.handleUpdateStatus(props.row)}>
                  停用
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '仓库代码',
          dataIndex: 'storageCode',
          sorter: true,
          filterType: 'input'
        },
        {
          title: '仓库名称',
          dataIndex: 'storageName',
          sorter: true,
          filterType: 'input'
        },
        {
          title: '仓库类型',
          dataIndex: 'storageType',
          sorter: true,
          filterType: 'checkbox',
          dictItems: this.createDictList('5000')
        },
        {
          title: '库位代码',
          dataIndex: 'storagePositionCode',
          sorter: true,
          filterType: 'input'
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          sorter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{props.row.isValid ? this.createDictText(props.row.isValid, '1004') : ''}</span>;
          }
        },
        {
          title: '停用日期',
          dataIndex: 'stopDate',
          sorter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '操作人',
          dataIndex: 'updatedBy',
          sorter: true,
          filterType: 'input',
          render: props => {
            return <span>{this.getEmployeeName(props.row.updatedBy)}</span>;
          }
        },
        {
          title: '操作时间',
          dataIndex: 'updatedAt',
          sorter: true,
          filterType: 'date-range'
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '仓库名称',
          fieldName: 'storageCode',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        }
      ];
    },
    // Excel更新导入
    showImportFun() {
      this.showImport = true;
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    addMoreHandle() {
      this.visible1 = true;
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    // 点击查询重置
    handleFilterChange(val) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign(this.fetchParams, val, {
        xhrAbort: false
      });
    },

    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}库位`;
      this.visible = true;
    },
    // 获取仓库列表
    async getBrands() {
      const res = await getStorageList({});
      // let resdata = await findAllRepair();
      // 增加非空判断
      if (res.data.length) {
        this.topFilterList.find(item => item.fieldName === 'storageCode').itemList = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
      }
      // this.topFilterList.find(item => item.fieldName === 'storageCode').initialValue=resdata.data['1100'];
    },
    // 点击停用启用
    async handleUpdateStatus(e) {
      this.$refs.table.START_LOADING();
      let res = await updateStatus({
        storageCode: e.storageCode,
        flag: e.isValid == 10041001 ? false : true,
        code: e.storagePositionCode
      });
      this.$refs.table.STOP_LOADING();
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    getEmployeeName(userId) {
      const item = this.serviceAdvisorData.find(x => {
        return x.value == userId;
      });
      return item ? item.text : '';
    },
    closeHandler(val, type) {
      this.visible1 = val;
      this.visible = val;
      this.showImport = val;
      // 重新加载
      this.handleResearchTable();
    },
    async getSalerList() {
      let resd = await getAllUser();
      // 增加非空判断
      if (resd.data.length) {
        this.serviceAdvisorData = resd.data.map(x => ({ text: x.employeeName, value: x.userId }));
      }
      let res = await getDealerUser();
      let obj = {};
      this.salerList = obj;
    },
    // 重新查询table数据
    handleResearchTable() {
      this.fetchParams.xhrAbort = true;
      this.fetchParams.xhrAbort = false;
    }
  }
};
</script>

<style lang="less" scoped></style>
